<?php

/**
 * 
 * @author yuxun
 *
 */
class ResourceController extends AdminBaseController {
	
	public function index(){
		$type = $this->R('type');
		if($type == 1){
			$this->setTitle('资源文件列表(正式)');
		}
		if($type == 2){
			$this->setTitle('资源文件列表(测试)');
		}
		if($type == 3){
			$this->setTitle('资源文件列表(审核)');
		}		
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
		
		$platformList = ResourceService::platformList();
		$platformList = array('' => '所有') + $platformList;
		
		$channelList = ResourceService::channelList();
		$channelList = array('' => '所有') + $channelList;
		
		$is_validList = ResourceService::is_validList();
		$is_validList = array('' => '所有') + $is_validList;
		
		$filter->addFilter('platform', 'select', PubFunc::t('平台'), array('options' => $platformList, 'value' => key($platformList)));
		$filter->addFilter('channel', 'select', PubFunc::t('平台'), array('options' => $channelList, 'value' => key($channelList)));
		$filter->addFilter('is_valid', 'select', PubFunc::t('平台'), array('options' => $is_validList, 'value' => key($is_validList)));
	
	
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$params['type'] = $this->R('type');
		$header = ResourceService::indexHeader($params);
		if(PubFunc::isAjax()){
			$result = ResourceService::indexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=resource/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'resource/index',
					'length'=>50,
					'page'=>$page
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
			$this->out['type'] = $this->R('type');
		}
	}
	
	public function add(){
		$type = $this->R('type');
		if($type == 1){
			$this->setTitle('上传资源文件(正式)');
		}
		
		if($type == 2){
			$this->setTitle('上传资源文件(测试)');
		}
		
		if($type == 3){
			$this->setTitle('上传资源文件(审核)');
		}

		
		$url = PubFunc::buildUrl("/oss/?r=resource/importResource");
		$this->out['url'] = $url;
		
		$platformList = ResourceService::platformList();
		$this->out['platformList'] = $platformList;
		
		$channelList = ResourceService::channelList($type);
		$this->out['channelList'] = $channelList;
		
		$this->out['type'] = $type;
	}

	
	public function importResource(){
		$upload = ResourceService::upload($_FILES);
		if($upload["status"] == 1){ //上传成功
			$out = $upload;
		}else{
			$out = $upload; //上传失败
		}
	
		$this->out = $out;
		$this->outType = 'json';
	}
	
	public function doAdd(){
		$type = $this->R('type');
		$result = ResourceService::doAdd($_POST);
		if($result['state'] !== false){
			$this->showMessage($result['msg'], PubFunc::buildUrl("/oss/?r=resource/index&type=".$type));
		}else{
			$this->showMessage($result['msg']);
		}	
	}
	
	public function edit(){
		$id = $this->R('id');
		$info = ResourceService::get($id);
	
		if(empty($info)) $this->showMessage('找不到该信息');
		$this->out['info'] = $info;
		
		$type = $this->R('type');
		if($type == 1){
			$this->setTitle('上传资源文件(正式编辑)');
		}
		
		if($type == 2){
			$this->setTitle('上传资源文件(测试编辑)');
		}
		
		if($type == 3){
			$this->setTitle('上传资源文件(审核编辑)');
		}
		
		
		$url = PubFunc::buildUrl("/oss/?r=resource/importResource");
		$this->out['url'] = $url;
		
		$platformList = ResourceService::platformList();
		$this->out['platformList'] = $platformList;
		
		$channelList = ResourceService::channelList($type);
		$this->out['channelList'] = $channelList;
		
		$this->out['type'] = $type;
	}
	
	public function doEdit(){
		$id = $this->R('id');
		$type = $this->R('type');
		$result = ResourceService::doEdit($id,$_POST);
		if($result['state'] !== false){
			$this->showMessage($result['msg'], PubFunc::buildBackUrl("/oss/?r=resource/index&type=".$type));
		}else{
			$this->showMessage($result['msg']);
		}
	}
	
	public function change_visable(){
		$id = $this->R('id');
		$val = $this->R('val');
		
		if($val == 1){  //修改为无效
			$out = array('status'=>0,'msg'=>'不能修改为无效!','data'=>'');
		}else if($val == 2){ //修改为有效
			$result = ResourceService::change_field($id,'is_valid',$val);
			
			if(!$result){
				$out = array('status'=>0,'msg'=>'修改失败','data'=>'');
			}else{
				// 记录到文件中
				$out = ResourceService::record($id);
			}
			
		}else{
			$out = array('status'=>0,'msg'=>'非法参数!','data'=>'');
		}
		$this->out = $out;
		$this->outType = 'json';
	}
	
	public function settingAutomatic(){
		if(PubFunc::isAjax()){
			$ids = PubRequest::R('ids');
			$automatic_time = PubRequest::R('automatic_time');
			$out = ResourceService::settingAutomatic($ids,$automatic_time);
			$this->out = $out;
			$this->outType = 'json';
		}else{
			$this->layout = '';
		
		}
	}
	



}